package com.catering.controller;

import com.alibaba.fastjson.JSON;
import com.catering.entity.CateringCart;
import com.catering.service.CateringCartService;
import com.catering.until.CommonUtils;
import com.catering.until.Log;
import com.catering.until.ResultData;
import com.catering.until.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/cateringCart")
public class CateringCartController {
	
	@Autowired
	private CateringCartService cateringCartSevice;
	
	@RequestMapping("/select")
	public ResultData select(@RequestBody Map<String,String> map) {
		return cateringCartSevice.select(map);
	}
	
	@Log(operation = "新增" , remark="【购物车况】新增购物车况" , type = "3")
	@RequestMapping("/insert")
	public ResultData insert(@RequestBody Map<String,String> map) {
		return cateringCartSevice.insert(map);
	}

	@Log(operation = "新增" , remark="【购物车况】新增批量购物" , type = "3")
	@RequestMapping("/insertBach")
	public ResultData insertBach(@RequestBody Map<String,Object> map) {
		try {
			CateringCart vo = JSON.parseObject(JSON.toJSONString(map), CateringCart.class);
			//先删除该用户购物车的所有信息
			cateringCartSevice.delete(map);
			List<CateringCart> voli = vo.getCartslist();
			for(CateringCart cart : voli) cart.setCreateTime(CommonUtils.getTime(19));
			cateringCartSevice.saveBatch(voli);
			return ResultData.success();
		}catch (Exception e) {
			e.printStackTrace();
			return ResultData.error(ResultEnum.INSERT_ERROR.getCode(),ResultEnum.INSERT_ERROR.getMsg());
		}
	}

	@Log(operation = "修改" , remark="【购物车况】修改购物车况" , type = "3")
	@RequestMapping("/update")
	public ResultData update(@RequestBody Map<String,String> map) {
		return cateringCartSevice.update(map);
	}
	
	@Log(operation = "删除" , remark="【购物车况】删除购物车况" , type = "3")
	@RequestMapping("/delete")
	public ResultData delete(@RequestBody Map<String,Object> map) {
		return cateringCartSevice.delete(map);
	}
	
}
